package com.wslNB.goods.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wslNB.entity.PageResult;
import com.wslNB.goods.dao.BrandMapper;
import com.wslNB.goods.pojo.Brand;
import com.wslNB.goods.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * 品牌业务层
 */
@Service
public class BrandServiceImpl implements BrandService {


    @Autowired
    private BrandMapper brandMapper;


    /**
     * 查询品牌列表
     *
     * @return List<Brand>
     */
    @Override
    public List<Brand> findList() {

        List<Brand> brands = brandMapper.selectAll();
        return brands;
    }


    /**
     * 根据ID查询品牌数据
     *
     * @param id
     * @return
     */
    @Override
    public Brand findById(Integer id) {

        return brandMapper.selectByPrimaryKey(id);
    }


    /**
     * 新增品牌
     *
     * @param brand
     */
    @Transactional
    @Override
    public void add(Brand brand) {
        brandMapper.insert(brand);
    }


    /**
     * 修改品牌
     *
     * @param brand
     */
    @Transactional
    @Override
    public void updateBrand(Brand brand) {
        //根据ID查询是否为null
        Brand brandFlag = brandMapper.selectByPrimaryKey(brand.getId());
        if (brandFlag != null) {
            brandMapper.updateByPrimaryKey(brand);
        }
    }


    /**
     * 根据ID删除品牌
     *
     * @param id
     */
    @Transactional
    @Override
    public void deleteBrand(Integer id) {
        Brand brand = brandMapper.selectByPrimaryKey(id);
        if (brand != null) {
            brandMapper.deleteByPrimaryKey(id);
            return;
        }
        throw new RuntimeException("未查询到数据，不能删除");
    }


    /**
     * 根据条件查询
     *
     * @param brand name and letter
     * @return
     */
    @Override
    public List<Brand> findByNameAndLetter(Brand brand) {

        Example example = new Example(Brand.class);
        Example.Criteria criteria = example.createCriteria();

        if (brand != null) {
            if (brand.getName() != null && "".equals(brand.getName())) {
                criteria.andEqualTo("name", brand.getName());
            }
            if (brand.getLetter() != null && "".equals(brand.getLetter())) {
                criteria.andEqualTo("letter", brand.getLetter());
            }
        }
        List<Brand> brands = brandMapper.selectByExample(example);

        return brands;
    }


    /**
     * 分页查询
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize,Brand brand) {

        PageHelper.startPage(currentPage,pageSize);
        //2.实例化自定义类，用来设置条件
        Example example = new Example(Brand.class);
        //3.设置条件的标准类
        Example.Criteria criteria = example.createCriteria();
        //3.1 设置品牌名称
        if(brand!=null) {
            if (brand.getName() != null && "".equals(brand.getName())) {
                criteria.andEqualTo("name", brand.getName());
            }
            if(brand.getLetter()!=null){
                criteria.andEqualTo("letter",brand.getLetter());
            }
        }

        Page<Brand> brandList = (Page<Brand>) brandMapper.selectByExample(example);
        long total = brandList.getTotal();
        List<Brand> list = brandList.getResult();

        return new PageResult(total,list);
    }


    //根据分类名称查询品牌列表
    @Override
    public List<Brand> findListByCategoryName(String categoryName) {
        return brandMapper.findListByCategoryName(categoryName);
    }


}
